package action.admin.account;

import beans.Account;
import dao.CheckAccount;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "createAcc", urlPatterns = {"/createAcc"})
public class createAcc extends HttpServlet {

    String username = "";
    String password = "";
    String full_name = "";
    String gender = "";
    String address = "";
    String phone = "";
    String email = "";
    String roll = "";
    String acc_id = "";    

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            CheckAccount ck = new CheckAccount();
            username = request.getParameter("username");
            password = request.getParameter("password");
            full_name = request.getParameter("full_name");
            gender = request.getParameter("gender");
            address = request.getParameter("address");
            phone = request.getParameter("phone");
            email = request.getParameter("email");
            roll = request.getParameter("roll");            
            String msg = "";
            String error = "";
            if (!username.equals("") && username != null) {
                if (!password.equals("") && password != null) {
                    if (!ck.checkUsername(username)) {
                        if (!full_name.equals("") && full_name != null) {
                            if (roll.equals("admin") || roll.equals("staff") || roll.equals("student")) {
                                Account account = new Account();
                                account.setUser_name(username);
                                account.setPass_word(password);
                                account.setFull_name(full_name);
                                account.setGender(gender);
                                account.setAddress(address);
                                account.setPhone(phone);
                                account.setEmail(email);
                                account.setRoll(roll);                                
                                if (ck.createAccount(account)) {
                                    msg = "Create account successful!";
                                    request.setAttribute("msg", msg); 
                                    request.setAttribute("roll", roll);
                                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=account");
                                    rd.forward(request, response);
                                } else {
                                    error = "Create error !";
                                    request.setAttribute("error", error);                                    
                                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=createuser");
                                    rd.forward(request, response);
                                }
                            } else {
                                error = "Select roll!";
                                request.setAttribute("error", error);
                                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=createuser");
                                rd.forward(request, response);
                            }
                        } else {
                            error = "Insert full name";
                            request.setAttribute("error", error);
                            RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=createuser");
                            rd.forward(request, response);
                        }
                    } else {
                        error = "Username has exist !";
                        request.setAttribute("error", error);
                        RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=createuser");
                        rd.forward(request, response);
                    }
                } else {
                    error = "Insert password !";
                    request.setAttribute("error", error);
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=createuser");
                    rd.forward(request, response);
                }
            } else {
                error = "Insert user name !";
                request.setAttribute("error", error);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=createuser");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
